package com.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ht.domain.bo.TbDoctorBO;
import com.ht.domain.po.*;
import com.ht.mapper.*;
import com.ht.service.TbDoctorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TbDoctorServiceImpl implements TbDoctorService {
    @Autowired
    private TbDoctorMapper tbDoctorMapper;

    @Autowired
    private TbDepartmentMapper tbDepartmentMapper;

    @Autowired
    private TbHospitalMapper tbHospitalMapper;

    @Autowired
    private TbLevelMapper tbLevelMapper;

    @Autowired
    private TbKnowledgeMapper tbKnowledgeMapper;

    @Override
    public List<TbDoctorBO> queryDoctorBydepartmentIdAndStatus(Long departmentId, Integer status, Long levelId,Long score ) {

        //根据科室名称查询科室表
        QueryWrapper<TbDepartment> q = new QueryWrapper();
        q.eq("id",departmentId);
        TbDepartment tbDepartmentPO = tbDepartmentMapper.selectOne(q);
        //根据科室id找出所有对应的医生
        QueryWrapper<TbDoctor> qq = new QueryWrapper();
        qq.eq("department_id",departmentId);
        qq.eq("inquiry_status","1");
        if (status == 0){
            qq.eq("inquiry_status",1);
        } else if (status == 1){
            qq.orderByAsc("inquiry_pic_price");
        } else if (status == 2){
            qq.orderByDesc("inquiry_pic_price");
        }
        if (levelId != 0){
            qq.eq("level",levelId);
        }
        if (score.intValue() == 1){
            qq.orderByDesc("score");
        }
        List<TbDoctor> list = tbDoctorMapper.selectList(qq);
        List<TbDoctorBO> listBO = new ArrayList<>();
        for (TbDoctor po : list) {
            TbDoctorBO bo = new TbDoctorBO();
            bo.setDepartmentName(tbDepartmentPO.getDepartmentName());//科室名称
            //根据医院id查询医院名称
            QueryWrapper<TbHospital> hq = new QueryWrapper<>();
            hq.eq("id",po.getHospitalId());
            TbHospital tbHospital = tbHospitalMapper.selectOne(hq);
            bo.setHospitalName(tbHospital.getHospitalName());
            //根据level查询药师的职位
            QueryWrapper<TbLevel> lq = new QueryWrapper<>();
            lq.eq("id",po.getLevel());
            TbLevel tbLevel = tbLevelMapper.selectOne(lq);
            bo.setLevelName(tbLevel.getLevel());
            BeanUtils.copyProperties(po,bo);
            listBO.add(bo);
        }
        return listBO;
    }

    @Override
    public List<TbDoctorBO> queryDoctorByFuzzy(String name, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum,pageSize);
        //知识表---病症
        QueryWrapper know = new QueryWrapper();
        know.like("knowledge_name",name);
        List<TbKnowledge> knowList = tbKnowledgeMapper.selectList(know);
        //科室表
        QueryWrapper department = new QueryWrapper();
        department.like("department_name",name);
        List<TbDepartment> departmentList = tbDepartmentMapper.selectList(department);
        //医院表
        QueryWrapper hospital = new QueryWrapper();
        hospital.like("hospital_name",name);
        List<TbHospital> hospitalList = tbHospitalMapper.selectList(hospital);
        //医生表
        QueryWrapper doctor = new QueryWrapper();
        doctor.like("doctor_name",name);
        departmentList.forEach(obj->{
            doctor.eq("department_id",obj.getId());
        });
        hospitalList.forEach(objj->{
            doctor.eq("hospital_id",objj.getId());
        });
        Page<TbDoctor> page1 = tbDoctorMapper.selectPage(page,doctor);
        List<TbDoctor> list = page1.getRecords();
        List<TbDoctorBO> listt = new ArrayList<>();
        list.forEach(objjj->{
            TbDoctorBO bo = new TbDoctorBO();
            BeanUtils.copyProperties(objjj,bo);
            listt.add(bo);
        });
        return listt;
    }
}
